import { getspecslist,specscount } from "@/request/api";
const state = {
    specslist : [],
    page:1,
    size:4,
    count:0
};
const getters = {
    getSpecslist(state){
        return state.specslist
    },
    getSize(state){
        return state.size
    },
    getCount(state){ 
        return state.count
    },
};
const mutations = {
    REQ_SPECSLIST(state,payload){
        state.specslist = payload
    },
    REQ_PAGENUM(state,payload){
        state.page = payload
    },
    REQ_COUNT(state,payload){
        state.count = payload
    },
};
const actions = {
    getSpecslistAction(context){
        getspecslist({
            page:context.state.page,
            size:context.state.size
        }).then(res=>{
            if(res.code==200){
                context.commit('REQ_SPECSLIST',res.list)
                if (context.state.page != 1 && res.list.length == 0) {
                    context.dispatch('changePageAction',context.state.page-1)
                }
            }
        })
    },
    getCountAction({commit}){
        specscount().then(res=>{
            // console.log(res,'/////');
            if(res.code==200){
                commit('REQ_COUNT',res.list[0].total)
            }
        })
    },
    changePageAction(context,payload){
        context.commit('REQ_PAGENUM',payload)
        context.dispatch('getSpecslistAction')
    }
};
export default{
    state,
    getters,
    mutations,
    actions,
    namespaced:true
}